package org.dentalcard.ui.security;

import org.dentalcard.entity.DecUser;
import org.dentalcard.entity.Shift;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.ui.api.tools.DecDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 15:43
 */
public class AddNewUserPopup extends DecDialog {

    public AddNewUserPopup(Frame owner) {
        super(owner, "Add New User", true);
        setSize(new Dimension(300, 300));
        setResizable(true);
        center(owner);
        init();
    }

    private void init() {
        JPanel main = new JPanel(new GridBagLayout());
        int rowN = 0;
        main.add(new JLabel("Name: "), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        final JTextField name = new JTextField("name");
        main.add(name, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        rowN++;

        main.add(new JLabel("Password: "), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        final JTextField password = new JTextField("password");
        main.add(password, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        rowN++;
        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    DecUser user = new DecUser();
                    user.setName(name.getText());
                    user.setPassword(password.getText());
                    PersistenceManager.saveOrUpdate(user);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(AddNewUserPopup.this, "Cannot save User.");
                }
                dispose();
            }
        });
        main.add(ok, new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        main.add(cancel, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        this.getContentPane().add(main);
    }
}
